package com.common.android.ui.widget;

import java.util.Calendar;

import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

import com.common.android.ui.wheel.NumericWheelAdapter;
import com.common.android.ui.wheel.OnWheelScrollListener;
import com.common.android.ui.wheel.WheelView;
import com.nd.android.u.allcommon.R;

public class DatePopupWindow extends PopupWindow implements OnWheelScrollListener, OnClickListener{
	public interface onDateSetListener{
		void onDateSet(int year,int month,int day);
	}
	public final static int DPW_TYPE_NORMAL = 0;
	public final static int DPW_TYPE_NO_DAY = 1;

	private WheelView mWheelYear;
	private WheelView mWheelMonth;

	private Button m_btnSetdateId;
	private Button m_btnSetCancardateId;
	
	private View m_contentView;
	private int mYear;
	private int mMonth;
	private onDateSetListener mDateSetListener;
	public DatePopupWindow(View contentView,int width, int height, boolean focusable) {
		super(contentView, width, height, focusable);
		m_contentView = contentView;
		SetCtrl();
		InitData();
	}
	
	void InitData() {
		initYear();
		initMonth();
	}
	
	public void setOnDateSetListener(onDateSetListener listener){
		mDateSetListener=listener;
	}
	
	void SetCtrl() {
		mWheelYear = (WheelView) m_contentView.findViewById(R.id.dateYearId);
		mWheelMonth = (WheelView) m_contentView.findViewById(R.id.datemonthId);

		m_btnSetdateId = (Button) m_contentView.findViewById(R.id.setdateId);
		m_btnSetCancardateId = (Button) m_contentView.findViewById(R.id.setCancardateId);

		m_btnSetdateId.setOnClickListener(this);
		m_btnSetCancardateId.setOnClickListener(this);
	}

	/**
	 * @brief 【初始化年滚轮】
	 *
	 * @n<b>函数名称</b>     :initYear
	 * @return    void   
	 * @<b>作者</b>          :  陈希
	 * @<b>修改</b>          :
	 * @<b>创建时间</b>      :  2012-5-8下午03:32:53
	 * @<b>修改时间</b>      :
	*/
	void initYear() {
		initWheel(mWheelYear, 1900, 2049, false);
	}
	
	/**
	 * @brief 【初始化月滚轮】
	 *
	 * @n<b>函数名称</b>     :initMonth
	 * @return    void   
	 * @<b>作者</b>          :  陈希
	 * @<b>修改</b>          :
	 * @<b>创建时间</b>      :  2012-5-8下午04:51:41
	 * @<b>修改时间</b>      :
	*/
	void initMonth() {
		initWheel(mWheelMonth, 1, 12, false);
	}
	
	/**
	 * @brief 【初始化滚轮】
	 *
	 * @n<b>函数名称</b>     :initWheel
	 * @param wheelView
	 * @param iStart
	 * @param iEnd
	 * @param bCyclic
	 * @return
	 * @return    NumericWheelAdapter   
	 * @<b>作者</b>          :  陈希
	 * @<b>修改</b>          :
	 * @<b>创建时间</b>      :  2012-5-8下午03:29:40
	 * @<b>修改时间</b>      :
	*/
	NumericWheelAdapter initWheel(WheelView wheelView, int iStart, int iEnd, boolean bCyclic) {

		NumericWheelAdapter wheelAdapter = new NumericWheelAdapter(m_contentView.getContext(),
				iStart, iEnd, "%02d");
		wheelAdapter.setTextColor(Color.BLACK);
		wheelView.setViewAdapter(wheelAdapter);
		wheelView.setCyclic(bCyclic);
		wheelView.setVisibleItems(3);
		wheelView.addScrollingListener(this);
		
		return wheelAdapter;
	}
	
	@Override
	public void onClick(View v) {
		int id=v.getId();
		if (id==R.id.setdateId){
			if (mDateSetListener!=null){
				mDateSetListener.onDateSet(mYear, mMonth, 0);
			}
			dismiss();
		} else if (id==R.id.setCancardateId){
			dismiss();
		} 
	}

	public void SetDateInfo(int year,int month) {
		mYear=year;
		mMonth=month;
		refreshWheels();
	}

	public void SetToday() {
		mYear=Calendar.getInstance().get(Calendar.YEAR);
		mMonth=Calendar.getInstance().get(Calendar.MONTH)+1;
		refreshWheels();
	}

	private void refreshWheels() {
		if (mWheelYear != null) {
			mWheelYear.setCurrentItem(mYear - 1900, false);
		}
		
		if (mWheelMonth != null) {
			mWheelMonth.setCurrentItem(mMonth - 1, false);
		}
	}

	/**
	 * @brief
	 * 
	 * 
	 * @n<b>函数名称</b> :onScrollingStarted
	 * @see
	 * 
	 * @param @param wheel
	 * @<b>作者</b> : 陈希
	 * @<b>创建时间</b> : 2012-5-8下午03:22:28
	 */

	@Override
	public void onScrollingStarted(WheelView wheel) {
		
	}

	/**
	 * @brief
	 * 
	 * 
	 * @n<b>函数名称</b> :onScrollingFinished
	 * @see
	 * 
	 * @param @param wheel
	 * @<b>作者</b> : 陈希
	 * @<b>创建时间</b> : 2012-5-8下午03:22:28
	 */

	@Override
	public void onScrollingFinished(WheelView wheel) {
		int id=wheel.getId();
		if (id==R.id.dateYearId){
			mYear=wheel.getCurrentItem() + 1900;
		}else if (id==R.id.datemonthId){
			mMonth=wheel.getCurrentItem() + 1;
		}
	}
}
